package com.yzh.pleaSys.utils;


import java.util.Random;

import static com.yzh.pleaSys.entity.DataDict.alphabet;
import static com.yzh.pleaSys.entity.DataDict.number;

/**
 * 生产验证码工具类
 */
public class CodeUtil {


    /**
     * @return 生成字母数字混合4位验证码
     */
    public static String getCode() {
        String str;
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            int random = (int) (Math.random() * 10);
            if (random % 2 == 0) {
                str = number;
            } else {
                str = alphabet;
            }
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * @return 生成纯数字6位验证码
     */
    public static String getNumCode() {
        StringBuilder sb = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            char ch = number.charAt(new Random().nextInt(number.length()));
            sb.append(ch);
        }
        return sb.toString();
    }
}
